<?php

App::uses('AppController', 'Controller');
App::uses('Member', 'Model');
App::uses('ChoirFunction', 'Model');

class MembersController extends AppController {
	
	public $uses = array('Member', 'ChoirFunction');
	
	public $paginate = array(
		'limit' => ITEMS_PER_PAGE,
		'order' => array(
			'Member.name'=>'asc'
		),
        'recursive' => 2,
        'joins' => array(
            'LEFT JOIN (
                members_functions MemberFunction
                INNER JOIN functions Function ON MemberFunction.function_id=Function.id
            ) ON MemberFunction.member_id=Member.id'
        ),
        'group' => 'Member.id'
	);
	
	public function index() {
        $this->_assignCommonData();
        
        $filter = $this->data['Filter'];
        if (empty($filter)) {
        	$filter = @$this->params['named']['Filter'];
        }
        
        $conditions = $this->Member->getConditionsForFilter($filter);
        $members = $this->paginate('Member', $conditions);
		$this->set(compact("members", "filter"));
	}

	private function _assignCommonData() {
		// kontakt types
		$columns = $this->Member->MemberContact->getColumnTypes();
		eval('$contact_types = ' . str_replace("enum", "array", $columns["contact_type"]) . ';' );
		$this->set("contact_types", Set::combine($contact_types, '{n}', '{n}'));
		
		//functions
		$this->set("functions", $this->ChoirFunction->find("list"));
		
		// voices, educations, categories
		$columns = $this->Member->getColumnTypes();
		eval('$voices = ' . str_replace("enum", "array", $columns["voice"]) . ';' );
		eval('$educations = ' . str_replace("enum", "array", $columns["education"]) . ';' );
		eval('$categories = ' . str_replace("enum", "array", $columns["category"]) . ';' );
		$this->set("voices", $voices);
		$this->set("educations", $educations);
		$this->set("categories", $categories);
		
	}
	
	public function create() {
		$this->_assignCommonData();
	}
	
	public function edit($id) {
		$this->_assignCommonData();
		
		$this->set("data", $this->Member->find("first", array("conditions" => array("Member.id" => $id))));
	}
	
	public function save() {
		$this->_assignCommonData();
		
		if($this->request->is('post')) {
			
			$this->Member->create();
			//pr($this->data);
			$this->Member->refactorData($this->data);
			if($this->Member->validates()) {
				$this->Member->deleteRelationsData();
				if($this->Member->saveAll($this->Member->data)) {
					$this->setFlash(strtotitle(__('saved')) . ".", "ok");
					return $this->redirect(array('action'=>'edit', $this->Member->id));
				}else{
					$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
					$this->set("data", $this->data);
					$this->set("validationErrors", $this->Member->validationErrors);
					return $this->render('edit');
				}
			}else{
				$this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
				$this->set("data", $this->data);
				return $this->render('edit');
			}
		}
		return $this->redirect(array('action'=>'index'));
	}
	
	public function delete($id) {
		$this->Member->deleteRelationsData($id);
		$this->Member->delete($id);
		$this->redirect(array('action'=>'index'));
	}

	public function stampa() {
	    $this->layout = 'blank';
	    $members = $this->Member->find('all', array('recursive'=>2));
	    
	    $this->set(compact('members'));
	}
}

?>
